package Forms;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.BoxLayout;
import javax.swing.JTable;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;

import net.proteanit.sql.DbUtils;

public class UserInfoPanel extends JPanel {
	private JTable bookingHistoryTable;
	private JLabel firstName;
	private JLabel lastName;
	private JLabel birthDate;
	private JLabel address;
	private JLabel nationality;
	private JLabel passengerID;

	Connection connection;

	/**
	 * Create the panel.
	 * Panel that aids the user, with his own info, could habe added an infoeditor,thougth about it too late
	 * the user can also see his booking history
	 */
	public UserInfoPanel(Connection _connection, String userID) {
		connection = _connection;
		setBackground(Color.LIGHT_GRAY);
		setLayout(new BorderLayout(0, 0));

		JPanel panel_9 = new JPanel();
		add(panel_9, BorderLayout.NORTH);
		panel_9.setLayout(new GridLayout(0, 2, 0, 0));

		JPanel panel_8 = new JPanel();
		panel_9.add(panel_8);
		panel_8.setLayout(new BoxLayout(panel_8, BoxLayout.Y_AXIS));

		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new EmptyBorder(0, 10, 0, 0));
		panel_8.add(panel_1);
		panel_1.setLayout(new GridLayout(0, 1, 0, 0));

		JLabel lblFirstName = new JLabel("First name:");
		lblFirstName.setFont(new Font("Tahoma", Font.BOLD, 11));
		panel_1.add(lblFirstName);

		firstName = new JLabel("---");
		panel_1.add(firstName);

		JSeparator separator = new JSeparator();
		panel_8.add(separator);

		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new EmptyBorder(0, 10, 0, 0));
		panel_8.add(panel_2);
		panel_2.setLayout(new GridLayout(0, 1, 0, 0));

		JLabel lblLastName = new JLabel("Last name:");
		lblLastName.setFont(new Font("Tahoma", Font.BOLD, 11));
		panel_2.add(lblLastName);

		lastName = new JLabel("---");
		panel_2.add(lastName);

		JSeparator separator_1 = new JSeparator();
		panel_8.add(separator_1);

		JPanel panel_6 = new JPanel();
		panel_6.setBorder(new EmptyBorder(0, 10, 0, 0));
		panel_8.add(panel_6);
		panel_6.setLayout(new GridLayout(0, 1, 0, 0));

		JLabel lblPassengerid = new JLabel("PassengerID:");
		lblPassengerid.setFont(new Font("Tahoma", Font.BOLD, 11));
		panel_6.add(lblPassengerid);

		passengerID = new JLabel("---");
		panel_6.add(passengerID);

		JSeparator separator_5 = new JSeparator();
		panel_8.add(separator_5);

		JPanel panel_7 = new JPanel();
		panel_9.add(panel_7);
		panel_7.setLayout(new BoxLayout(panel_7, BoxLayout.Y_AXIS));

		JPanel panel_4 = new JPanel();
		panel_7.add(panel_4);
		panel_4.setLayout(new GridLayout(0, 1, 0, 0));

		JLabel lblNationality = new JLabel("Nationality:");
		lblNationality.setFont(new Font("Tahoma", Font.BOLD, 11));
		panel_4.add(lblNationality);

		nationality = new JLabel("---");
		panel_4.add(nationality);

		JSeparator separator_2 = new JSeparator();
		panel_7.add(separator_2);

		JPanel panel_3 = new JPanel();
		panel_7.add(panel_3);
		panel_3.setLayout(new GridLayout(0, 1, 0, 0));

		JLabel lblAddress = new JLabel("Address:");
		lblAddress.setFont(new Font("Tahoma", Font.BOLD, 11));
		panel_3.add(lblAddress);

		address = new JLabel("---");
		panel_3.add(address);

		JSeparator separator_3 = new JSeparator();
		panel_7.add(separator_3);

		JPanel panel_5 = new JPanel();
		panel_7.add(panel_5);
		panel_5.setLayout(new GridLayout(0, 1, 0, 0));

		JLabel lblBirthdate = new JLabel("Birth date:");
		lblBirthdate.setFont(new Font("Tahoma", Font.BOLD, 11));
		panel_5.add(lblBirthdate);

		birthDate = new JLabel("---");
		panel_5.add(birthDate);

		JSeparator separator_4 = new JSeparator();
		panel_7.add(separator_4);

		JPanel panel_10 = new JPanel();
		add(panel_10);
		panel_10.setLayout(new BorderLayout(0, 0));

		bookingHistoryTable = new JTable();
		panel_10.add(new JScrollPane(bookingHistoryTable));
		bookingHistoryTable.setEnabled(false);
		JLabel lblBookingHistory = new JLabel("Booking History:");
		panel_10.add(lblBookingHistory, BorderLayout.NORTH);


	}

	//adds the userID-specific data to the table and labels
	public void addData(String userID, Connection _connection) {
		connection = _connection;
		try {
			Statement statement = connection.createStatement();
			ResultSet resultSet = statement
					.executeQuery("SELECT * FROM passenger WHERE passengerid = '"
							+ userID + "'");
			resultSet.next();
			firstName.setText(resultSet.getString(1));
			lastName.setText(resultSet.getString(2));
			birthDate.setText(resultSet.getString(3));
			address.setText(resultSet.getString(4));
			nationality.setText(resultSet.getString(5));
			passengerID.setText(resultSet.getString(6));

			fillTable(userID);

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
//fills the table with the booking history
	public void fillTable(String userID) {
		Statement statement;
		try {
			statement = connection.createStatement();

			ResultSet resultSet = statement
					.executeQuery("SELECT * FROM trip WHERE tripid  in (SELECT trip FROM booking WHERE passenger = '"
							+ userID + "')");

			bookingHistoryTable.setModel(DbUtils
					.resultSetToTableModel(resultSet));

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	


}
